<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="style.css" />

<!-- メインシーンシェーダ -->
<script id="main-vs" type="x-shader/x-vertex-shader">
attribute vec3 position;
attribute vec4 color;
attribute vec2 textureCoord;

uniform mat4 mvpMatrix;

varying vec4 vColor;
varying vec2 vTextureCoord;

void main() {
    vColor = color;
    vTextureCoord = textureCoord;
    gl_Position = mvpMatrix * vec4(position, 1.0);
}
</script>

<script id="main-fs" type="x-shader/x-fragment-shader">
precision mediump float;

uniform sampler2D texture;

varying vec4 vColor;
varying vec2 vTextureCoord;

void main() {
    vec4 texel = texture2D(texture, vTextureCoord);
    gl_FragColor = texel;
}
</script>

<!-- 高輝度抜き出しシェーダ -->
<script id="brightness-vs" type="x-shader/x-vertex-shader">
attribute vec3 position;
attribute vec2 textureCoord;

varying vec2 vTextureCoord;

void main() {
    vTextureCoord = textureCoord;
    gl_Position = vec4(position, 1.0);
}
</script>
<script id="brightness-fs" type="x-shader/x-fragment-shader">
precision mediump float;

uniform sampler2D texture;
uniform float minBright;

varying vec2 vTextureCoord;

void main() {
    vec3 texel = max(vec3(0.0), (texture2D(texture, vTextureCoord) - minBright).rgb);
    gl_FragColor = vec4(texel, 1.0);
}
</script>

<!-- Bloomシェーダ -->
<script id="bloom-vs" type="x-shader/x-vertex-shader">
attribute vec3 position;
attribute vec2 textureCoord;

varying vec2 vTextureCoord;

void main() {
    vTextureCoord = textureCoord;
    gl_Position = vec4(position, 1.0);
}
</script>
<script id="bloom-fs" type="x-shader/x-fragment-shader">
precision mediump float;

uniform sampler2D texture;

#define SAMPLE_COUNT 15
uniform vec2 offsetsH[SAMPLE_COUNT];
uniform float weightsH[SAMPLE_COUNT];
uniform vec2 offsetsV[SAMPLE_COUNT];
uniform float weightsV[SAMPLE_COUNT];

uniform bool isVertical;

varying vec2 vTextureCoord;

void main() {
    vec4 color = vec4(0.0);
    if (isVertical) {
        for (int i = 0; i < SAMPLE_COUNT; i++) {
            color += texture2D(texture, vTextureCoord + offsetsV[i]) * weightsV[i];
        }
    }
    else {
        for (int i = 0; i < SAMPLE_COUNT; i++) {
            color += texture2D(texture, vTextureCoord + offsetsH[i]) * weightsH[i];
        }
    }
    gl_FragColor = vec4(color.rgb, 1.0);
}
</script>

<!-- 結果画面シェーダ -->
<script id="result-vs" type="x-shader/x-fragment-shader">
attribute vec3 position;
attribute vec2 textureCoord;

varying vec2 vTextureCoord;

void main() {
    vTextureCoord = textureCoord;
    gl_Position = vec4(position, 1.0);
}
</script>
<script id="result-fs" type="x-shader/x-fragment-shader">
precision mediump float;

uniform sampler2D originalTexture;
uniform sampler2D bloomTexture;
uniform float toneScale;

varying vec2 vTextureCoord;

void main() {
    vec4 texel = vec4(0.0);
    texel  = texture2D(originalTexture, vTextureCoord) * toneScale;
    texel += texture2D(bloomTexture, vTextureCoord);
    gl_FragColor = texel;
}
</script>
</head>
<body>

    <div id="ctrl"><input type="range" min="0.0" max="1.0" step="0.01" id="brightSlider"></div>
    
    <script src="js/Stats.js"></script>
    <script src="js/mathjs.js"></script>
    <script src="js/lib.js"></script>
    <script src="js/main.js"></script>
</body>
</html>
